package com.definesys.smecrailtransit.module.SiteDetails.adapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.BR;
import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.SmecManager.SmecAnimation.SmecAnimation;
import com.definesys.smecrailtransit.base.SmecBaseRecyclerViewHolder;
import com.definesys.smecrailtransit.databinding.ItemSiteFaultBinding;
import com.definesys.smecrailtransit.databinding.ItemSiteRunTimeBinding;
import com.definesys.smecrailtransit.module.MonthForm.MonthFormPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.ELeStationBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleStationRunTimeBean;
import com.definesys.smecrailtransit.module.SiteDetails.dialog.SiteRunDialog;
import com.definesys.smecrailtransit.module.TotalTime.TotalTimePresenter;
import com.definesys.smecrailtransit.utils.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by xupeizuo on 2017/8/24.
 */

public class SiteDetailsAdapter extends RecyclerView.Adapter<SmecBaseRecyclerViewHolder>{


    public static final int VIEW_TYPE_RUN_TIME=1001;
    public static final int VIEW_TYPE_FAULT=1002;
    private int mViewType;
    private Context mContext;
    private DisplayMetrics displayMetrics;
    private ArrayList<EleStationRunTimeBean> runTimeBeen=new ArrayList<>();
    private ArrayList<ELeStationBean> eLeStationBeanArrayList=new ArrayList<>();

    private int MAX_WIDTH=0;
    private double MAX_LENDTH=0;
    private MonthFormPresenter monthFormPresenter;
    private TotalTimePresenter totalTimePresenter;
    private SiteRunDialog siteRunDialog;
    private SiteRunDialog siteRunDialog1;
    private HashMap<String, Object> param1 = new HashMap<>();
    private HashMap<String, Object> param = new HashMap<>();

    public SiteDetailsAdapter(Context context, int viewType){
        this.mContext=context;
        this.mViewType=viewType;
        this.displayMetrics=context.getResources().getDisplayMetrics();
        MAX_WIDTH=displayMetrics.widthPixels/7;
    }

    @Override
    public SmecBaseRecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mViewType == VIEW_TYPE_RUN_TIME){
            ItemSiteRunTimeBinding itemSiteRunTimeBinding= DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_site_run_time,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new SmecBaseRecyclerViewHolder(itemSiteRunTimeBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(itemSiteRunTimeBinding);
            return smecBaseRecyclerViewHolder;
        }

        if(mViewType == VIEW_TYPE_FAULT){
            ItemSiteFaultBinding itemSiteFaultBinding= DataBindingUtil.inflate(LayoutInflater.from(mContext),R.layout.item_site_fault,parent,false);
            SmecBaseRecyclerViewHolder smecBaseRecyclerViewHolder=new SmecBaseRecyclerViewHolder(itemSiteFaultBinding.getRoot());
            smecBaseRecyclerViewHolder.setViewType(viewType);
            smecBaseRecyclerViewHolder.setBinding(itemSiteFaultBinding);
            return smecBaseRecyclerViewHolder;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(SmecBaseRecyclerViewHolder holder, int position) {
        holder.setIsRecyclable(false);
        if(mViewType == VIEW_TYPE_RUN_TIME){
            holder.getBinding().setVariable(BR.runTime,runTimeBeen.get(position));
            ItemSiteRunTimeBinding itemSiteRunTimeBinding=(ItemSiteRunTimeBinding)holder.getBinding();
            int needWidth=getWidth(runTimeBeen.get(position).getValue());
            ValueAnimator valueAnimator=SmecAnimation.showValueAnimator(0,needWidth,500);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int widthed =(Integer)valueAnimator.getAnimatedValue();
                    itemSiteRunTimeBinding.pillar.getLayoutParams().width=widthed;
                    itemSiteRunTimeBinding.pillar.requestLayout();
                }
            });
            valueAnimator.start();

            itemSiteRunTimeBinding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    siteRunDialog1=new SiteRunDialog(mContext,R.style.project_back,1);
                    if(totalTimePresenter == null){
                        totalTimePresenter=new TotalTimePresenter(mContext);
                    }
                    siteRunDialog1.setEleNo(runTimeBeen.get(position).getEleCode());
                    siteRunDialog1.setEleContractNo(runTimeBeen.get(position).getEleContractNo());
                    param1.put("eleContractNo",runTimeBeen.get(position).getEleContractNo());
                    param1.put("from",runTimeBeen.get(position).getFrom());
                    param1.put("to",runTimeBeen.get(position).getTo());
                    totalTimePresenter.getElevatorTime(param1);
                    siteRunDialog1.show();
                }
            });
        }

        if(mViewType == VIEW_TYPE_FAULT){
            holder.getBinding().setVariable(BR.fault,eLeStationBeanArrayList.get(position));
            ItemSiteFaultBinding itemSiteFaultBinding=(ItemSiteFaultBinding)holder.getBinding();
            int needWidth=getWidth(eLeStationBeanArrayList.get(position).getValue());
            ValueAnimator valueAnimator=SmecAnimation.showValueAnimator(0,needWidth,500);
            valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    int widthed =(Integer)valueAnimator.getAnimatedValue();
                    itemSiteFaultBinding.pillarFault.getLayoutParams().width=widthed;
                    itemSiteFaultBinding.pillarFault.requestLayout();
                }
            });
            valueAnimator.start();
            itemSiteFaultBinding.getRoot().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(monthFormPresenter == null){
                        monthFormPresenter=new MonthFormPresenter(mContext);
                    }
                    siteRunDialog=new SiteRunDialog(mContext,R.style.project_back,0);
                    siteRunDialog.setEleNo(eLeStationBeanArrayList.get(position).getEleCode());
                    siteRunDialog.setEleContractNo(eLeStationBeanArrayList.get(position).getEleContractNo());
                    param.put("eleContractNo",eLeStationBeanArrayList.get(position).getEleContractNo());
                    param.put("month",6);
                    monthFormPresenter.getFaultLog(param);
                    siteRunDialog.show();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        if(mViewType == VIEW_TYPE_RUN_TIME){
            return runTimeBeen.size();
        }
        if(mViewType == VIEW_TYPE_FAULT){
            return eLeStationBeanArrayList.size();
        }
        return 0;
    }

    public void setRunTimeBeen(ArrayList<EleStationRunTimeBean> runTimeBeen) {
        MAX_LENDTH=0;
        if(CommonUtils.notEmpty(runTimeBeen)){
            this.runTimeBeen = runTimeBeen;
            for(EleStationRunTimeBean eleStationRunTimeBean : runTimeBeen){
                _getMaxWidth(eleStationRunTimeBean.getValue());
            }
            notifyDataSetChanged();
        }else {
            this.runTimeBeen =new ArrayList<>();
            notifyDataSetChanged();
        }
    }

    public  void seteLeStationBeanArrayList(ArrayList<ELeStationBean> leStationBeanArrayList){
        MAX_LENDTH=0;
        if(CommonUtils.notEmpty(leStationBeanArrayList)){
            this.eLeStationBeanArrayList=leStationBeanArrayList;
            for(ELeStationBean eLeStationBean : leStationBeanArrayList){
                _getMaxWidth(eLeStationBean.getValue());
            }
            notifyDataSetChanged();
        }else {
            eLeStationBeanArrayList=new ArrayList<>();
            notifyDataSetChanged();
        }
    }

    private void  _getMaxWidth(double value){
        MAX_LENDTH=Math.max(value,MAX_LENDTH);
    }

    /**
     * 获取宽度
     * @param value
     * @return
     */
    private int getWidth(double value){
        if(value == 0){
            return 0;
        }
        return (int)(MAX_WIDTH*(value/MAX_LENDTH));
    }
}
